"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const kurento_client_1 = __importDefault(require("kurento-client"));
const config_1 = require("../config/config");
const { wsUri } = config_1.lConfig.kurentoServer;
class default_1 {
    static async getClient(callback) {
        return await (0, kurento_client_1.default)(wsUri).catch(error => {
            if (error) {
                console.error('Kurento: => ', "Could not find media server at address" + wsUri
                    + ". Exiting with error " + error);
            }
        });
    }
    async connect(fromEndpoint, toEndpoint, callback) {
        return await fromEndpoint.connect(toEndpoint).catch((error) => {
            console.error('connect: => ', error);
        });
    }
    async initEndpoint(client, cb) {
        this.createMediaPipeline(client, (pipeline) => {
            this.createEndpoint(pipeline, (endpoint) => {
                if (cb)
                    cb(endpoint, pipeline);
            });
        });
    }
    async initPlayerEndpoint(client, uri, cb) {
        return this.createMediaPipeline(client, (pipeline) => {
            return this.createPlayerEndpoint(pipeline, uri, (player) => {
                if (cb)
                    cb(player, pipeline);
                return { player, pipeline };
            });
        });
    }
    onIceCandidate(webRtcEndpoint, cb) {
        return webRtcEndpoint.on('IceCandidateFound', (event) => {
            var iceCandidate = kurento_client_1.default.getComplexType('IceCandidate')(event.candidate);
            cb && cb(iceCandidate);
            return iceCandidate;
        });
    }
    async createMediaPipeline(client, cb) {
        return await client.create('MediaPipeline').catch((error) => {
            console.error('createMediaPipeline: => ', error);
        });
    }
    async createEndpoint(pipeline, cb) {
        return await pipeline.create('WebRtcEndpoint').catch((error) => {
            console.error('createEndpoint: => ', error);
        });
    }
    async createPlayerEndpoint(pipeline, uri, cb) {
        return await pipeline.create('PlayerEndpoint', { uri }).catch((error) => {
            console.error('PlayerEndpoint: => ', error);
        });
    }
    async createRecordEndpoint(pipeline, uri, cb) {
        return await pipeline.create('RecorderEndpoint', { uri }).catch((error) => {
            console.error('RecorderEndpoint: => ', error);
        });
    }
    async processOffer(endpoint, sdpOffer, cb) {
        endpoint.gatherCandidates((error) => {
            if (error) {
                console.error('gatherCandidates: => ', error);
            }
        });
        return await endpoint.processOffer(sdpOffer).catch((error) => {
            console.error('processOffer: => ', error);
        });
    }
    async stop(members, socket, userId) {
    }
}
exports.default = default_1;
